/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.energy.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.vo.ErrorMessage;
import com.excesys.exsecs.energy.entity.BumDevEnroll;
import com.excesys.exsecs.energy.mapper.DevEnrollMapper;
import com.excesys.exsecs.energy.service.DevEnrollService;
import com.excesys.exsecs.energy.vo.DevEnrollExcelImportVO;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 设备注册
 */
@Service
public class DevEnrollServiceImpl extends ServiceImpl<DevEnrollMapper, BumDevEnroll> implements DevEnrollService {


	@Override
	public R importBamSBuildings(List<DevEnrollExcelImportVO> excelVOList, BindingResult bindingResult) {
		// 通用校验获取失败的数据
		List<ErrorMessage> errorMessageList = (List<ErrorMessage>) bindingResult.getTarget();
		int sucess = 0;
		int fail = 0;

		// 执行数据插入操作 组装 RoleDto
		for (DevEnrollExcelImportVO excel : excelVOList) {
			// 个性化校验逻辑
			List<BumDevEnroll> postList = this.list();
			// 检验区域或者NFC编码是否存在
			Set<String> errorMsg = new HashSet<>();
			boolean existPost = postList.stream().anyMatch(
					post -> excel.getUnitNo().equals(post.getUnitNo()) || excel.getDevName().equals(post.getDevName()));
			if (existPost) {
				errorMsg.add("设备编码或名称存在重复" + excel.getUnitNo() + "," + excel.getDevName());
			}

			// 数据合法情况
			if (CollUtil.isEmpty(errorMsg)) {
				int num = insertBamSBuildings(excel);
				if (num == 1) {
					fail++;
				}
				else {
					sucess++;
				}
			}
			else {
				// 数据不合法情况
				errorMessageList.add(new ErrorMessage(excel.getLineNum(), errorMsg));
			}
		}
		if (CollUtil.isNotEmpty(errorMessageList)) {
			return R.failed(errorMessageList);
		}
		return R.ok(sucess, "成功导入数量" + sucess + "。" + "存在" + fail + "重复");
	}

	private int insertBamSBuildings(DevEnrollExcelImportVO excel) {
		BumDevEnroll bumDevEnroll = new BumDevEnroll();
		bumDevEnroll.setUnitNo(excel.getUnitNo());
		bumDevEnroll.setDevName(excel.getDevName());
		this.save(bumDevEnroll);
		return 0;
	}
}
